package cas.ihep.alluxio.impl;

import alluxio.URI;

import java.util.Objects;

public class MyStandardUri implements URI {

    private java.net.URI mUri;

    MyStandardUri(java.net.URI uri){
        mUri=uri.normalize();
    }

    @Override
    public String getAuthority() {
        return mUri.getAuthority();
    }

    @Override
    public String getHost() {
        return mUri.getHost();
    }

    @Override
    public String getPath() {
        return mUri.getPath();
    }

    @Override
    public String getQuery() {
        return mUri.getQuery();
    }

    @Override
    public int getPort() {
        return mUri.getPort();
    }

    @Override
    public String getScheme() {
        return mUri.getScheme();
    }

    @Override
    public boolean isAbsolute() {
        return mUri.isAbsolute();
    }

    @Override
    public java.net.URI getBaseURI() {
        return mUri;
    }

    @Override
    public int compareTo(URI other) {
        if(other==null){
            return 1;
        }
        int compare = this.compareScheme(other);
        return compare != 0 ? compare : this.mUri.compareTo(other.getBaseURI());
    }

    private int compareScheme(URI other) {
        String scheme = this.getScheme();
        String otherScheme = other.getScheme();
        if (scheme == null && otherScheme == null) {
            return 0;
        } else if (scheme != null) {
            return otherScheme != null ? scheme.compareToIgnoreCase(otherScheme) : 1;
        } else {
            return -1;
        }
    }
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        } else if (!(o instanceof MyStandardUri)) {
            return false;
        } else {
            MyStandardUri that = (MyStandardUri)o;
            return this.compareScheme(that) == 0 && this.mUri.equals(that.getBaseURI());
        }
    }

    public int hashCode() {
        return Objects.hash(this.mUri, this.getScheme());
    }
}
